//import 'package:amap_base/amap_base.dart';

import 'dart:convert';

import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_hotwork/bean/BaseBean.dart';
import 'package:flutter_hotwork/bean/TimedyaBaobiaoBean.dart';
import 'package:flutter_hotwork/home/RoadChoice.dart';
import 'package:flutter_hotwork/utils/DioUtils.dart';

import '../LoadingDialog.dart';
import '../Login.dart';
import '../Toast.dart';

class timedayvolumebaobiaoPage extends StatefulWidget {
  @override
  timedaybaobiaoState createState() => new timedaybaobiaoState();
}

class timedaybaobiaoState extends State<timedayvolumebaobiaoPage> {
  Map selectArea;
  bool hasdata = false;
  TimedyaBaobiaoBean hotuserbean;
  double totalleiji = 0.0;
  double totalchaobiao = 0.0;
  double totalxiuzheng = 0.0;
  double totaljiesuan = 0.0;

  ///接收选择的结果
  void handleSelect(Map targetArea) {
    setState(() {
      selectArea = targetArea;
      getribaobiaoList();
      // initData(selectArea['proviceIndex'],selectArea['cityIndex']);
    });
  }

  @override
  void initState() {
    super.initState();
    DioUtils.createInstance();
  }

  @override
  void dispose() {
//    _amapLocation.stopLocate();
    super.dispose();
  }

  var time = formatDate(new DateTime.now(), [yyyy, '-', mm, "-", dd]);
  var time2 = formatDate(new DateTime.now(), [yyyy, '-', mm]);

  void getribaobiaoList() async {
    showDialog(
        context: context,
        builder: (context) {
          return new LoadingDialog();
        });
    // Navigator.pop(context);
    try {
      Response response;
      response = await DioUtils.dio.get(DioUtils.API_PREFIX +
          "/timeFlowReportDay/listTimeDayReport?queryDate=" +
          time2 +
          "-01" +
          "&id=" +
          selectArea['id']);
      Map respMap = json.decode(response.data);
      basebean base = basebean.fromJson(respMap);
      Navigator.pop(context);
      if (base.code == 0) {
        hasdata = true;
        hotuserbean = TimedyaBaobiaoBean.fromJson(respMap);
        totalleiji = 0.00;
        totalchaobiao = 0.00;
        totalxiuzheng = 0.00;
        totaljiesuan = 0.00;
        for (int i = 0; i < hotuserbean.result.timeDayReportDtos.length; i++) {
          // totalleiji+=hotuserbean.statisticsDayReportDtoList[i].accumulation;
          if (hotuserbean.result.timeDayReportDtos[i].dayOfDate == "小计") {
            totalchaobiao += hotuserbean.result.timeDayReportDtos[i]
                .timeFlowReportDayVos[0].dailyAmount;
            totalxiuzheng += hotuserbean.result.timeDayReportDtos[i]
                .timeFlowReportDayVos[0].correctionAmount;
            totaljiesuan += hotuserbean.result.timeDayReportDtos[i]
                .timeFlowReportDayVos[0].settlementAmount;
          }
        }
        setState(() {});
      } else if (base.code == 500) {
        LocalToast.toast(context, msg: "会话超时，请重新登陆！");
        Navigator.pushAndRemoveUntil(
          context,
          new MaterialPageRoute(builder: (context) => new MyHomePage()),
          (route) => route == null,
        );
      } else {
        LocalToast.toast(context, msg: base.message);
      }
    } catch (e) {
      Navigator.pop(context);
      LocalToast.toast(context, msg: "请选择用户");
    }
  }

  @override
  Widget build(BuildContext context) {
    ScrollController controller = ScrollController();
    double rpx = MediaQuery.of(context).size.width / 700;
    return Scaffold(
        body: new CustomScrollView(shrinkWrap: true,
            // 内容
            slivers: <Widget>[
          new SliverPadding(
              padding: const EdgeInsets.all(0),
              sliver: new SliverList(
                  delegate: new SliverChildListDelegate(
                <Widget>[
                  Container(
                    child: Row(
                      children: [
                        Container(
                          margin: EdgeInsets.only(
                              left: 0, right: 0, top: 5, bottom: 5),
                          child: InkWell(
                            onTap: () {
                              showBottomSheet(
                                  context: context,
                                  builder: (_) => Container(
                                        height: 200.0,
                                        child: AreaSelection(
                                          onSelect: handleSelect,
                                          initProviceIndex: selectArea == null
                                              ? 0
                                              : selectArea['proviceIndex'],
                                          initCityIndex: selectArea == null
                                              ? 0
                                              : selectArea['cityIndex'],
                                          // initCountyIndex: selectArea == null ? 0 : selectArea['countyIndex'],
                                        ),
                                      ));
                            },
                            child: Container(
                                height: 20,
                                margin: EdgeInsets.only(
                                    left: 10, right: 0, top: 0, bottom: 0),
                                padding: EdgeInsets.only(
                                    left: 2, right: 0, top: 0, bottom: 0),
                                decoration: BoxDecoration(
                                  border: new Border.all(
                                      color: Colors.blue, width: 0.5),
                                  // 边色与边宽度
                                  color: Colors.white, // 底色
                                  //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Container(
                                      width: 100,
                                      child: Text(
                                        selectArea != null
                                            ? selectArea['address']
                                            : "请选择用户",
                                        style: TextStyle(
                                            color: Colors.black, fontSize: 10),
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                    Container(
                                      width: 5,
                                    ),
                                    Image.asset(
                                      'images/jiantou.png',
                                      width: 14.0,
                                      height: 20.0,
                                    ),
                                  ],
                                )),
                          ),
                        ),
                        FlatButton(
                          onPressed: () {
                            DatePicker.showDatePicker(context,
                                // 是否展示顶部操作按钮
                                showTitleActions: true,
                                // 最小时间
                                minTime: DateTime(2018, 3, 5),
                                // 最大时间
                                maxTime: DateTime(2099, 6, 7),
                                // change事件
                                onChanged: (date) {
                              print('change $date');
                            },
                                // 确定事件
                                onConfirm: (date) {
                              time = formatDate(date, [yyyy, '-', mm, "-", dd]);
                              time2 = formatDate(date, [yyyy, '-', mm]);
                              setState(() {
                                getribaobiaoList();
                              });
                              print('confirm $time');
                            },
                                // 当前时间
                                currentTime: DateTime.now(),
                                // 语言
                                locale: LocaleType.zh);
                          },
                          child: Container(
                              width: 100,
                              height: 20,
                              margin: EdgeInsets.only(
                                  left: 10, right: 0, top: 0, bottom: 0),
                              padding: EdgeInsets.only(
                                  left: 10, right: 0, top: 0, bottom: 0),
                              decoration: BoxDecoration(
                                border: new Border.all(
                                    color: Colors.blue, width: 0.5),
                                // 边色与边宽度
                                color: Colors.white, // 底色
                                //        borderRadius: new BorderRadius.circular((20.0)), // 圆角度
                              ),
                              child: Row(
                                children: [
                                  Image.asset(
                                    'images/rili.png',
                                    width: 14.0,
                                    height: 20.0,
                                  ),
                                  Container(
                                    width: 5,
                                  ),
                                  Text(
                                    '$time2',
                                    style: TextStyle(
                                        color: Colors.black, fontSize: 10),
                                  )
                                ],
                              )),
                        ),
                        InkWell(
                          onTap: () {
                            getribaobiaoList();
                          },
                          child: Container(
                              width: 50,
                              height: 20,
                              margin: EdgeInsets.only(
                                  left: 10, right: 0, top: 0, bottom: 0),
                              padding: EdgeInsets.only(
                                  left: 2, right: 2, top: 0, bottom: 0),
                              decoration: BoxDecoration(
                                // border: new Border.all(color: Colors.blue, width: 0.5), // 边色与边宽度
                                color: Colors.blue, // 底色
                                borderRadius:
                                    new BorderRadius.circular((5.0)), // 圆角度
                              ),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    "查询",
                                    style: TextStyle(
                                        color: Colors.white, fontSize: 10),
                                  )
                                ],
                              )),
                        ),
                      ],
                    ),
                  ),

                  Row(
                    children: [
                      Flexible(fit: FlexFit.tight, child: SizedBox()),
                      Text(
                        "用户分时每日汽量",
                        style: TextStyle(
                            color: Colors.black87,
                            fontSize: 12,
                            fontWeight: FontWeight.bold),
                      ),
                      Flexible(fit: FlexFit.tight, child: SizedBox()),
                    ],
                  ),

                  //根据是否有数据展示
                  hasdata
                      ? Container(
                          child: Column(children: [
                            Container(
                              padding: EdgeInsets.only(
                                  left: 10, right: 10, top: 10, bottom: 0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                mainAxisSize: MainAxisSize.max,
//                交叉轴的布局方式，对于column来说就是水平方向的布局方式
                                crossAxisAlignment: CrossAxisAlignment.center,
                                //就是字child的垂直布局方向，向上还是向下
                                verticalDirection: VerticalDirection.down,
                                children: [
                                  Container(
                                    height: 40,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Text(
                                          "日期",
                                          style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 8,
                                          ),
                                        ),
                                        Container(
                                          height: 3,
                                        ),
                                        Text(
                                          "$time2",
                                          style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 8,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Flexible(
                                      fit: FlexFit.tight, child: SizedBox()),
                                  Container(
                                    height: 40,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Text(
                                          "用热单位",
                                          style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 8,
                                          ),
                                        ),
                                        Container(
                                          height: 3,
                                        ),
                                        Container(
                                          width: 100,
                                          child: Text(
                                            selectArea != null
                                                ? selectArea['address']
                                                : "请选择用户",
                                            style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 10),
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Flexible(
                                      fit: FlexFit.tight, child: SizedBox()),
                                  Container(
                                    height: 40,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Text(
                                          "售热量",
                                          style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 8,
                                          ),
                                        ),
                                        Container(
                                          height: 3,
                                        ),
                                        Text(
                                          hotuserbean.result.caloriesSold
                                              .toString(),
                                          style: TextStyle(
                                              color: Colors.blue, fontSize: 8),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Flexible(
                                      fit: FlexFit.tight, child: SizedBox()),
                                  Container(
                                    height: 40,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Text(
                                          "上月底累计数",
                                          style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 8,
                                          ),
                                        ),
                                        Container(
                                          height: 3,
                                        ),
                                        Text(
                                          hotuserbean.result.lastAccumulation
                                              .toString(),
                                          style: TextStyle(
                                              color: Colors.blue, fontSize: 8),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Flexible(
                                      fit: FlexFit.tight, child: SizedBox()),
                                  Container(
                                    height: 40,
                                    child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.center,
                                      children: [
                                        Text(
                                          "NO",
                                          style: TextStyle(
                                            color: Colors.blue,
                                            fontSize: 8,
                                          ),
                                        ),
                                        Container(
                                          height: 3,
                                        ),
                                        Text(
                                          hotuserbean.result.serialNumber ==
                                                  null
                                              ? ""
                                              : hotuserbean.result.serialNumber
                                                  .toString(),
                                          style: TextStyle(
                                              color: Colors.blue, fontSize: 8),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              width: rpx * 730,
                              height: 0.5,
                              color: Colors.grey,
                            ),
                            Container(
                              width: rpx * 730,
                              height: 5,
                            ),
                            Container(
                              padding: EdgeInsets.only(
                                  left: 10, right: 10, top: 0, bottom: 0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                mainAxisSize: MainAxisSize.max,
//                交叉轴的布局方式，对于column来说就是水平方向的布局方式
                                crossAxisAlignment: CrossAxisAlignment.center,
                                //就是字child的垂直布局方向，向上还是向下
                                verticalDirection: VerticalDirection.down,
                                children: [
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "日期",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 70,
                                    child: Text(
                                      "时间",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "累计数",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "抄表数(t)",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 60,
                                    child: Text(
                                      "修正量(t)",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "结算量(t)",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "备注",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              width: rpx * 750,
                              child:
                                  // Expanded(
                                  //   child:
                                  hotuserbean.result.timeDayReportDtos.length >
                                          0
                                      ? genContentList(
                                          hotuserbean.result.timeDayReportDtos,
                                          context,
                                          controller)
                                      : Container(),
                              // ),
                            ),
                            Container(
                              padding: EdgeInsets.only(
                                  left: 10, right: 10, top: 0, bottom: 0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                mainAxisSize: MainAxisSize.max,
//                交叉轴的布局方式，对于column来说就是水平方向的布局方式
                                crossAxisAlignment: CrossAxisAlignment.center,
                                //就是字child的垂直布局方向，向上还是向下
                                verticalDirection: VerticalDirection.down,
                                children: [
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "小计",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 70,
                                    child: Text(
                                      "--",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 70,
                                    child: Text(
                                      "--",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      totalchaobiao
                                          .toStringAsFixed(2)
                                          .toString(),
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 60,
                                    child: Text(
                                      totalxiuzheng
                                          .toStringAsFixed(2)
                                          .toString(),
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      totaljiesuan.toStringAsFixed(2),
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: 30,
                                    width: rpx * 100,
                                    child: Text(
                                      "",
                                      textAlign: TextAlign.center,
                                      style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 8,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ]),
                        ) //根据是否有数据展示
                      : Container()
                ],
              )))
        ]));
  }

  genContentList(
      List<TimeDayReportDtos> list, BuildContext context, controller) {
    double rpx = MediaQuery.of(context).size.width / 700;
    List<TimeFlowReportDayVos> data = new List();
    for (var i = 0; i < list.length; i++) {
      if (list[i].dayOfDate != "小计") {
        data.addAll(list[i].timeFlowReportDayVos);
      }
    }
    for (var i = 0; i < list.length; i++) {
      if (list[i].dayOfDate != "小计") {
        for (var j = 0; j < data.length; j++) {
          data[j].dayOfDate = list[i].dayOfDate;
        }
      }
    }
    return ListView.builder(
      shrinkWrap: true,
      controller: controller,
      itemCount: data.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          padding: EdgeInsets.only(left: 10, right: 10, top: 0, bottom: 0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
//                交叉轴的布局方式，对于column来说就是水平方向的布局方式
            crossAxisAlignment: CrossAxisAlignment.center,
            //就是字child的垂直布局方向，向上还是向下
            verticalDirection: VerticalDirection.down,
            children: [
              Container(
                height: 30,
                width: rpx * 100,
                child: Text(
                  data[index].dayOfDate,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
              Container(
                height: 30,
                width: rpx * 70,
                child: Text(
                  data[index].times,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
              Container(
                height: 30,
                width: rpx * 100,
                child: Text(
                  data[index].accumulation.toString(),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
              Container(
                height: 30,
                width: rpx * 60,
                child: Text(
                  data[index].dailyAmount.toString(),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
              Container(
                height: 30,
                width: rpx * 100,
                child: Text(
                  data[index].correctionAmount.toString(),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
              Container(
                height: 30,
                width: rpx * 100,
                child: Text(
                  data[index].settlementAmount.toString(),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
              Container(
                height: 30,
                width: rpx * 100,
                child: Text(
                  data[index].remark == null
                      ? ""
                      : data[index].remark.toString(),
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 8,
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
